import { ENTITY_MANAGER_KEY } from '@libs/common/interceptors';
import { DataSource, EntityManager, ObjectLiteral, Repository } from 'typeorm';

export class BaseRepository {
  constructor(private dataSource: DataSource) {}

  protected getRepository<T extends ObjectLiteral>(
    entityCls: new () => T,
  ): Repository<T> {
    const entityManager: EntityManager =
      this.dataSource[ENTITY_MANAGER_KEY] ?? this.dataSource.manager;
    return entityManager.getRepository(entityCls);
  }
}
